package com.example.ykbboom.universitytransportation.View;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.ykbboom.universitytransportation.Activity.RequestDetailActivity;
import com.example.ykbboom.universitytransportation.MyApplication;
import com.example.ykbboom.universitytransportation.POJO.MyRequestDetailItem;
import com.example.ykbboom.universitytransportation.POJO.RequestDetailItem;
import com.example.ykbboom.universitytransportation.R;
import com.example.ykbboom.universitytransportation.Util.SharedPrefUtil;

import java.util.List;

/**
 * Created by ykbboom on 2017/3/16 15:53
 * email：ykbpro@whut.edu.cn
 */

public class MyRequestDetailAdapter extends RecyclerView.Adapter<MyRequestDetailAdapter.ViewHolder> {

    private List<MyRequestDetailItem>list;
    private Context context = MyApplication.getContext();
    public MyRequestDetailAdapter(List<MyRequestDetailItem>list){
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.my_request_item,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.requestView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyRequestDetailItem item = list.get(viewHolder.getAdapterPosition());
                Intent intent = new Intent(v.getContext(), RequestDetailActivity.class);
                intent.putExtra("userName",item.getDriverName());
                intent.putExtra("userPhone",item.getDriverPhone());
                intent.putExtra("place","From "+item.getStartPlace()+"\nTo "+item.getStopPlace());
                intent.putExtra("description",item.getDescription());
                intent.putExtra("time",item.getTime());
                intent.putExtra("completeMsg",item.getCompleteMsg());
                intent.putExtra("picurl",item.getPicUrl());
                intent.putExtra("requestId",item.getRequestId());
                intent.putExtra("position",viewHolder.getAdapterPosition());
                intent.putExtra("activityName",v.getContext().getClass().getSimpleName());
                v.getContext().startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MyRequestDetailItem myRequestDetailItem = list.get(position);
        Glide.with(context).load(myRequestDetailItem.getPicUrl()).into(holder.img);
        holder.placeText.setText(myRequestDetailItem.getStartPlace());
        if (!myRequestDetailItem.getDriverName().equals(SharedPrefUtil.getUserName())){
            holder.nameText.setText(myRequestDetailItem.getDriverName());
        }else {
            holder.nameText.setText(SharedPrefUtil.getUserName());
        }
        holder.timeText.setText(myRequestDetailItem.getTime());
        holder.stateText.setText(myRequestDetailItem.getCompleteMsg());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView placeText;
        TextView nameText;
        TextView timeText;
        TextView stateText;
        View requestView;
        public ViewHolder(View view){
            super(view);
            requestView = view;
            img = (ImageView) view.findViewById(R.id.my_request_item_img);
            placeText = (TextView) view.findViewById(R.id.my_request_item_place);
            nameText = (TextView) view.findViewById(R.id.my_request_item_name);
            timeText = (TextView) view.findViewById(R.id.my_request_item_time);
            stateText = (TextView) view.findViewById(R.id.my_request_item_state);
        }
    }
}
